/**
 * 
 */
package com.user.query.utils;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.user.query.controller.SystemComponent;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * @author chuzz
 * 
 */
public class FileUtils {

	private static SimpleDateFormat ym_format = new SimpleDateFormat("yyyyMM");
	private static SimpleDateFormat ymd_format = new SimpleDateFormat("yyyy/MM/dd");
	private static Logger log = Logger.getLogger(FileUtils.class);
	public static String moveFile(HttpServletRequest request, String type,
			final String fileName) {
		String path = "";
		try {
			String host = request.getHeader("host");
			//String pathDir = request.getSession().getServletContext()
			//		.getRealPath("/static/upload");
			String targetPathDir = SystemComponent.getProperty("upload.imagePath");
			
			/*String targetPath = pathDir + File.separator + type
					+ File.separator + DateUtils.getCurrentDate("yyyyMMdd");*/
			//目标路径
			String targetFilePath = File.separator + type + fileName.replace("temp", "");
			String targetPath = targetPathDir + targetFilePath;
			
			//源文件路径
			/*String srcPathDir = request.getSession().getServletContext().
					getRealPath(SystemComponent.getProperty("upload.tempPath"));*/
			String srcPathDir = SystemComponent.getProperty("upload.tempPath") + fileName;
			//String srcPth = srcPathDir + File.separator + fileName;
			
			File srcFile = new File(srcPathDir);
			File destFile = new File(targetPath);
			srcFile.setWritable(true, false);
			destFile.setWritable(true, false);
			org.apache.commons.io.FileUtils.moveFile(srcFile, destFile);
//			path = targetPath.substring(targetPath.indexOf("upload") - 1);
//			path = path.replace("\\", "/");
			path = targetFilePath;
		} catch (Exception e) {
			path = "error";
			log.error("文件移动报错", e);
		}
		return path;
	}
	
	/**
	 * 移动文件，同时剪切
	 * @author chuzz
	 * @param request
	 * @param type
	 * @param fileName
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @return
	 * @return String
	 * @date 2015-11-11 下午7:08:08
	 */
	public static String moveFile(HttpServletRequest request, String type,
			String fileName ,int x, int y, int width, int height){
		String path = "";
		try {
			String host = request.getHeader("host");
			//String pathDir = request.getSession().getServletContext()
			//		.getRealPath("/static/upload");
			String targetPathDir = SystemComponent.getProperty("upload.imagePath");
			
			/*String targetPath = pathDir + File.separator + type
					+ File.separator + DateUtils.getCurrentDate("yyyyMMdd");*/
			//目标路径
			String targetFilePath = File.separator + type + fileName.replace("temp", "");
			String targetPath = targetPathDir + targetFilePath;
			
			//源文件路径
			/*String srcPathDir = request.getSession().getServletContext().
					getRealPath(SystemComponent.getProperty("upload.tempPath"));*/
			String srcPathDir = SystemComponent.getProperty("upload.tempPath") + fileName;
			//String srcPth = srcPathDir + File.separator + fileName;
			
			File srcFile = new File(srcPathDir);
			File destFile = new File(targetPath);
			srcFile.setWritable(true, false);
			destFile.setWritable(true, false);
			
			//剪切图片
			String postfix = fileName.substring(fileName.lastIndexOf(".")+1);
			FileInputStream is = new FileInputStream(srcFile);
			ImageInputStream imageIs = ImageIO.createImageInputStream(is);
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(postfix);
			ImageReader reader = it.next();
			reader.setInput(imageIs);
			ImageReadParam param = reader.getDefaultReadParam();
			Rectangle rect = new Rectangle(x, y, width, height); 
			param.setSourceRegion(rect);
			BufferedImage bi = reader.read(0, param);
			//写出图片
			ImageIO.write(bi, postfix, destFile);
			//org.apache.commons.io.FileUtils.moveFile(srcFile, destFile);
//			path = targetPath.substring(targetPath.indexOf("upload") - 1);
//			path = path.replace("\\", "/");
			path = targetFilePath;
		} catch (Exception e) {
			path = "";
			log.error("文件移动报错", e);
		}
		return path;
	}
	
	public static String moveFile(HttpServletRequest request, String type,
			File file) {
		String fileName = "";
		try {
			if(file!=null){
				String postfix = file.getName().substring(file.getName().lastIndexOf(".")).toLowerCase();
				String targetPathDir = SystemComponent.getProperty("upload.imagePath");
				String targetFilePath = File.separator+ type +File.separator + ym_format.format(new Date())
						+ File.separator + IDGenerator.getUUID()+postfix;
				File targetFile = new File(targetPathDir+targetFilePath);
				file.setWritable(true, false);
				targetFile.setWritable(true, false);
				org.apache.commons.io.FileUtils.moveFile(file, targetFile);
				fileName = targetFilePath;
			}
		} catch (IOException e) {
			e.printStackTrace();
			log.error("文件移动报错", e);
			fileName = "";
		}
		return fileName;
	}

}
